<%--
  Created by IntelliJ IDEA.
  User: app
  Date: 2018/7/28
  Time: 下午9:00
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>购买</title>
    <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" style="position: absolute;top: 30%;left: 50%;-ms-transform: translate(-50%,-50%);-moz-transform: translate(-50%,-50%);-o-transform: translate(-50%,-50%);transform: translate(-50%,-50%);width:40%;padding:20px;border-radius:5px;">

    <h2 style="color:rgb(239,177,40);">购买详情</h2>
    <br><br>
    <table class="table table-striped">
        <tr>
            <th style="width: 100px">图书ID</th>
            <th style="width: 100px">书名</th>
            <th style="width: 100px">价格</th>
            <th style="width: 100px">数量</th>
        </tr>

            <tr>
                <td class="bookId">${bookInfo.bid}</td>
                <td>${bookInfo.bname}</td>
                <td>${bookInfo.bprice}</td>
                <td><input class="subBtn" type="button" value="-" onclick="subBook(this)"><span class="bookCount">1</span><input class="addBtn" type="button" value="+"onclick="addBook(this)"></td>
            </tr>
    </table>
    <br>

    <label style="font-size: 20px">总价：</label><lable id="tar" style="font-size: 20px">${pricePerBook}</lable>
    <input type="button" value="提交订单" class="btn" style="float:right;margin-right: 20px;" onclick="forward()">

    <script>
        var countList = [];
        var idList = document.getElementsByClassName("bookId");

        function subBook(var1) {
            if(parseInt(var1.nextSibling.innerHTML)==1)
                return;
//        alert(var1.nextSibling);
            var text=parseInt(var1.nextSibling.innerHTML)-1;
            var1.nextSibling.innerHTML=text;
//       alert(document.getElementById("tar").innerHTML);
//        alert(var1.parentElement.parentElement.getElementsByTagName("td")[1].innerHTML);
            var price=parseFloat(document.getElementById("tar").innerHTML)-parseFloat(convert(var1.parentElement.parentElement.getElementsByTagName("td")[2].innerHTML));
            document.getElementById("tar").innerHTML=price.toFixed(2);
        }
        function addBook(var1) {
//        alert(var1.nextSibling);
            var text=parseInt(var1.previousSibling.innerHTML)+1;
            var1.previousSibling.innerHTML=text;
            var price=parseFloat(document.getElementById("tar").innerHTML)+parseFloat(convert(var1.parentElement.parentElement.getElementsByTagName("td")[2].innerHTML));
            document.getElementById("tar").innerHTML=price.toFixed(2);
        }

        function convert(var1) {
            return var1.replace(/[^(0-9|\.)]/ig,"");
        }

        function forward() {
            var price=parseFloat(document.getElementById("tar").innerHTML);
            var con=confirm("总共"+price+"元，请问是否付款？");
            if(con==true){
//                    window.location.href="paymentAction?totalPrice="+msg;
                $.ajax({
                        type: "POST",
                        url: "insertPaymentAction",
                        data:"totalPrice="+price +"&bid=${bookInfo.bid}",
                        error:function () {
                            alert("错误");
                        },
                        success:function (msg) {
                            var message = msg["message"]
                            alert(message);
                            if(message=="支付成功"){

                                window.location.href="homeAction"
                            }
                        }
                    }
                )
            }
        }


        function generateJson() {
            var dataJson;
            for(var i = 0;i<idList.length;i++){
                var idNode = idList[i];
                var currentNode = idNode.parentElement;
                var countNode = currentNode.getElementsByTagName("td")[3];
                var bookCount = countNode.getElementsByClassName("bookCount")[0].innerHTML;
                var content = {
                    "bookId":idNode.innerHTML,
                    "count" : bookCount
                };
                countList[i] = (content);
            }

            dataJson = JSON.stringify(countList, null, 0);
            return dataJson;
        }

    </script>
</div>
</body>
</html>

